package category

import (
	"context"
	"github.com/gin-gonic/gin"
	"goods_bff/controller"
	"goods_bff/forms"
	"goods_bff/goods_proto"
	"goods_bff/pkg"
	"net/http"
)

func CreateCategory(c *gin.Context) {
	//接收参数
	var form forms.CreateCategoryForm
	if err := c.ShouldBind(&form); err != nil {
		controller.ReturnErrorJson(err, c)
		return
	}
	comment := pkg.Comment()
	category, err := comment.CreateCategory(context.Background(), &goods_proto.CategoryInfoRequest{
		Name:           form.Name,
		ParentCategory: int32(form.ParentCategory),
		Level:          int32(form.Level),
		IsTab:          form.IsTab,
	})
	if err != nil {
		controller.HandleGrpcErrorToHttp(err, c)
		return
	}

	//返回响应
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "创建分类成功",
		"data": category,
	})
}
